import 'dart:convert';
import 'package:encrypt/encrypt.dart';

class EncryptUtil {
  //aes加密
  static String aesEncode(String content, password) {
    List<String> _password =
        (password + password + password + password).split('');
    List<int> bytes = _password.map((string) => int.parse(string)).toList();
    try {
      final key = Key.fromBase64(base64Encode(bytes));
      final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
      final encrypted =
          encrypter.encrypt(content, iv: IV.fromBase64(base64Encode(bytes)));
      return encrypted.base64;
    } catch (err) {
      return content;
    }
  }

  //aes解密
  static dynamic aesDecode(dynamic base64, password) {
    List<String> _password =
        (password + password + password + password).split('');
    List<int> bytes = _password.map((string) => int.parse(string)).toList();
    try {
      final key = Key.fromBase64(base64Encode(bytes));
      final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
      return encrypter.decrypt64(base64,
          iv: IV.fromBase64(base64Encode(bytes)));
    } catch (err) {
      return base64;
    }
  }
}
